﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FineUI;
using Maticsoft.DBUtility;


namespace wdgl.admin.sysAdmin
{
    public partial class Add : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //初始绑定
                sysrolepopedom_rname.DataTextField = "rname";
                sysrolepopedom_rname.DataValueField = "id";
                sysrolepopedom_rname.DataSource = DbHelperSQL.Query("select id,rname from sysrolepopedom");
                sysrolepopedom_rname.DataBind();
                

                if (Request["id"] != null) //修改
                {
                    try
                    {
                        mydddd.BLL.sysAdmin bll = new mydddd.BLL.sysAdmin();
                        mydddd.Model.sysAdmin mo = bll.GetModel(int.Parse(Request["id"]));

                        LoginName.Text = mo.LoginName;
                        RealName.Text = mo.RealName;
                        //Password.Text = mo.Password;
                        sysrolepopedom_rname.SelectedValue = mo.sysrolepopedom_rname.ToString();
                        Remark.Text = mo.Remark;
                        sort.Text = mo.sort.ToString();
                       
                    }
                    catch (Exception)
                    {
                        //Response.End();
                    }
                }
            }
        }

        protected void Button_save_OnClick(object sender, EventArgs e)
        {

            //注意数据类型类型，可以通过实体来确认类型给赋值
            //wdgl.Model.sysAdmin mo = new wdgl.Model.sysAdmin();
            //mo
            

            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic["LoginName"] = LoginName.Text;

            //验证
            if (Request["id"] == null)
            {
                DBControl dbControl = new DBControl();
                if (dbControl.Exists("sysAdmin", dic))
                {
                    Alert.Show("登录名称重复请换一个！");
                    return;
                }
            }

            dic["RealName"] = RealName.Text;
            
            dic["sysrolepopedom_rname"] = int.Parse(sysrolepopedom_rname.SelectedValue);
            dic["Remark"] = Remark.Text;
            dic["sort"] = int.Parse(sort.Text);
            //dic["isdel"] = int.Parse(isdel.Text);
        




            if (Request["id"] == null)//添加
            {
                dic["Password"] = Password.Text;

                if (InsertDB("sysAdmin", dic) > 0)
                {
                    AlertInfor("添加成功", true);
                }
                else
                {
                    Alert.Show("添加失败");
                }

            }
            else//修改
            {
                if (Password.Text.Length > 0)
                {
                    dic["Password"] = Password.Text;
                }

                Dictionary<string, object> dicWhere = new Dictionary<string, object>();

                dicWhere["ID"] = int.Parse(Request["id"]);


                if (UpdateDB("sysAdmin", dic, dicWhere))
                {
                    AlertInfor("修改成功", true);
                }
                else
                {
                    Alert.Show("修改失败");
                }
            }
        }


        private string GetArrayString(string[] array)
        {
            StringBuilder sb = new StringBuilder();
            foreach (string item in array)
            {
                sb.Append(item+",");
            }
            return sb.ToString().TrimEnd(',');
        }
    }
}